package com.game.league.entrance.login;

import android.text.TextUtils;
import android.view.View;

import com.game.base.app.base.BaseActivity;
import com.game.base.auth.AuthUtils;
import com.game.base.auth.constant.ThirdConstant;
import com.game.league.entrance.user.UserMgr;
import com.game.league.entrance.user.bean.WXUserInfo;
import com.game.league.entrance.user.table.User;
import com.game.base.utils.log.Logger;
import com.game.base.utils.toast.ToastUtil;
import com.game.league.R;
import com.game.league.databinding.ActivityLoginBinding;
import com.game.league.util.SkipUtil;

import org.json.JSONObject;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import tech.jianyue.auth.AuthCallback;
import tech.jianyue.auth.UserInfoForThird;

/**
 * 注册登录
 * Created by Su on 2019/5/21.
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements View.OnClickListener {
    private UserInfoForThird thirdInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        binding.setClick(this);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_qq:
                AuthUtils.loginQQ(this, new AuthCallback() {
                    @Override
                    public void onSuccessForLogin(UserInfoForThird info) {
                    }

                    @Override
                    public void onFailed(String code, String msg) {
                        Logger.d(code + ", " + msg);
                    }
                });
                break;

            case R.id.login_wx:
                AuthUtils.loginWX(this, new AuthCallback() {
                    @Override
                    public void onSuccessForLogin(UserInfoForThird info) {
                        thirdInfo = info;
                        UserMgr.thirdSignUpLogin(ThirdConstant.WEIXIN_SNS, info.aToken, info.expireIn, info.userId, listener);
                    }
                });
                break;
        }
    }

    private final LogInListener<JSONObject> listener = new LogInListener<JSONObject>() {
        @Override
        public void done(JSONObject jsonObject, BmobException e) {
            if (e == null) {
                SkipUtil.showMainClearTask(mActivity);
                User user = UserMgr.getCurrentUser();
                if (TextUtils.isEmpty(user.getNickname())) {  // 新用户
                    updateUserInfo(user);
                }
            } else {
                Logger.printThrowable(e);
            }
            ToastUtil.showShort(e == null ? "登录成功" : "登录失败");
        }
    };

    /**
     * 更新用户信息
     */
    private void updateUserInfo(User user) {
        WXUserInfo wxInfo = new WXUserInfo();
        wxInfo.parseJson(thirdInfo.userInfo);

        user.setNickname(wxInfo.getNickname());
        user.setGender(wxInfo.getGender());
        user.setAddress(wxInfo.getAddress());
        user.setAvatar(wxInfo.getAvatar());
        UserMgr.updateUser(user);
    }
}
